package com.wanghengzhi.kata;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 王潇
 * @since 2021/11/21 14:53
 */
public class Anagrams {
    public Set<String> generate(String word) {
        if (word.length() == 1) {
            return Collections.singleton(word);
        }

        Set<String> result = new HashSet<>();
        for (int i = 0; i < word.length(); i++) {
            char firstChar = word.charAt(i);
            String leftChars = removeCharAt(word, i);
            Set<String> generate = generate(leftChars);
            for (String leftChar : generate) {
                result.add(firstChar + leftChar);
            }
        }
        return result;
    }

    private String removeCharAt(String word, int index) {
        return new StringBuilder(word).deleteCharAt(index).toString();
    }

    private String reverse(String word) {
        return new StringBuilder(word).reverse().toString();
    }
}
